﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Model.Core
{
    public static class HttpPostHelp
    {

        /// <summary>
        /// 执行POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="postData"></param>
        /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
        /// <param name="headers">填充消息头</param>        
        /// <returns></returns>
        public static string HttpPost(string url, string paramData, Dictionary<string, string> headers = null, string contentType = "application/json", int timeOut = 30000)
        {
            using (HttpClient client = new HttpClient())
            {
                if (headers != null)
                {
                    foreach (var header in headers)
                        client.DefaultRequestHeaders.Add(header.Key, header.Value);
                }
                using (HttpContent httpContent = new StringContent(paramData, Encoding.UTF8))
                {
                    if (contentType != null)
                        httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
                    HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
                    return response.Content.ReadAsStringAsync().Result;
                }
            }
        }

        /// <summary>
        /// 执行POST请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="param"></param>
        /// <param name="contentType"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string HttpWebRequestPost(string url, string param, string contentType = "application/json", Dictionary<string, string> headers = null)
        {
            byte[] byteArray = Encoding.ASCII.GetBytes(param);
            HttpWebRequest req = SetHttpWebRequest(url, "POST", contentType, byteArray.Length);
            return GetResponseData(req, byteArray, Encoding.UTF8, headers);
        }

        /// <summary>
        /// 获取请求返回数据
        /// </summary>
        /// <param name="req"></param>
        /// <param name="byteArray"></param>
        /// <param name="headers"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetResponseData(HttpWebRequest req, byte[] byteArray, Encoding encoding, Dictionary<string, string> headers = null)
        {
            string responseData = String.Empty;
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    req.Headers.Add(item.Key, item.Value);
                }
            }
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(byteArray, 0, byteArray.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    responseData = reader.ReadToEnd();
                }
                return responseData;
            }
        }

        /// <summary>
        /// 设置HttpWebRequest
        /// </summary>
        /// <param name="method"></param>
        /// <param name="contentType"></param>
        /// <param name="contentLength"></param>
        /// <returns></returns>
        public static HttpWebRequest SetHttpWebRequest(string url, string method, string contentType, long contentLength)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.Method = method;
            req.ContentType = contentType;
            req.ContentLength = contentLength;
            return req;
        }

    }

    public class HttpInformation
    {
        /// <summary>
        /// 配置Url地址
        /// </summary>
        public string ApiUrl { get; set; }

        /// <summary>
        /// 请求
        /// </summary>
        public string ApiType { get; set; }

        /// <summary>
        /// API调用服务
        /// </summary>
        public string ApiServer { get; set; }

        /// <summary>
        /// API调用方法
        /// </summary>
        public string Apimethod { get; set; }

        /// <summary>
        /// API调用方法
        /// </summary>
        public string Params { get; set; }

        /// <summary>
        /// Post类型
        /// </summary>
        public string ContentType { get; set; } = "application/json";


        public int TimeOut { get; set; } = 30000;
    }
}
